﻿Imports Abyx.SVCMS.Library.DB
Imports Abyx.SVCMS.Library
Imports Abyx.WindowsExtendedControls


Public Class SVC_CustomerEntry_Form
    Implements ICarfDataEditingChildWindow

#Region "ICARF"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        If (DataObject IsNot Nothing) AndAlso (TypeOf DataObject Is CustomerType) Then
            Dim c1 As CustomerType = DataObject
            If c1 = Me.iMainDataObject Then
                Return Me
            Else
                Return Nothing
            End If
        Else
            Return Nothing
        End If
    End Function
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls
        AppSettings.MainForm.OrderRefreshRelatedDataControls(Me, _
                                     RelatedControlDataCategoriesEnum.Customers, _
                                     DataItem, _
                                     ActionOnDataItem)
    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        If sender IsNot Me Then
            Refresh_ControlsWithDataObject()
            AppSettings.MainForm.StatusInfoMsgPut("Customer information has been refreshed, because the data was updated else where")
        End If

    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        With Me.iMainDataObject
            Me.CustomerId_TextBox.Text = .CustomerId.ToString
            Me.CustomerNo_TextBox.Text = .CustomerNo
            Me.IdType_StylishComboBox.SelectedValue = .IdType
            Me.IdNo_TextBox.Text = .IdNo
            Me.Name_TextBox.Text = .Name
            Me.Address_TextBox.Text = .Address
            Me.Telephone_TextBox.Text = .Tel
            Me.Mobile_TextBox.Text = .Mob
        End With
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls
        With Me.iMainDataObject
            .CustomerId = New Guid(Me.CustomerId_TextBox.Text)
            .CustomerNo = Me.CustomerNo_TextBox.Text
            .IdType = Me.IdType_StylishComboBox.SelectedItem
            .IdNo = Me.IdNo_TextBox.Text
            .Name = Me.Name_TextBox.Text
            .Address = Me.Address_TextBox.Text
            .Tel = Me.Telephone_TextBox.Text
            .Mob = Me.Mobile_TextBox.Text
        End With
    End Sub
#End Region

#Region "Declarations"
    Dim iFormActionOnData As FormActionOnDataEnum
    Dim iMainDataObject As CustomerType = Nothing
#End Region

#Region "Overloads/Overrides"
    Public Overloads Sub Show(FormActionOnData As FormActionOnDataEnum, _
                              Optional DataObject As CustomerType = Nothing)
        Me.Show()
        If DataObject IsNot Nothing Then
            Me.iMainDataObject = DataObject
            Me.iFormActionOnData = FormActionOnDataEnum.UpdateRecord
            Me.Delete_ImageButton.Enabled = True
            Me.CustomerNo_TextBox.Enabled = False
            Me.GenerateCustomerNumber_ImageButton.Enabled = False
        Else
            Me.iMainDataObject = New CustomerType(Guid.NewGuid)
            Me.iMainDataObject.IdType = "NID"
            Me.iFormActionOnData = FormActionOnDataEnum.NewRecord
            Me.Delete_ImageButton.Enabled = False
            Me.CustomerNo_TextBox.Enabled = True
            Me.GenerateCustomerNumber_ImageButton.Enabled = True
            Me.GenerateCustomerNumber_ImageButton.PerformClick()
        End If
        Refresh_FixLists()
        Refresh_ControlsWithDataObject()
        Refresh_FormCaptions()
    End Sub
#End Region

#Region "Events"
    Private Sub SVC_CustomerEntry_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.InitializeThisFormAndControlSettings()
    End Sub
    Private Sub SVC_CustomerEntry_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Apply_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Apply_ImageButton.Click

        UpdateCustomerRecord()
        Me.OrderRefreshRelatedDataControls(Me.iMainDataObject, FormActionOnDataEnum.UpdateRecord)
        AppSettings.MainForm.StatusInfoMsgPut("Customer information has been updated")
        Me.Close()

    End Sub
    Private Sub Delete_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Delete_ImageButton.Click

        If MsgBox("Are you sure you want to delete this customer information", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = Windows.Forms.DialogResult.Yes Then
            DeleteCustomerRecord()
            Me.OrderRefreshRelatedDataControls()
            AppSettings.MainForm.StatusInfoMsgPut("Customer information has been deleted")
            Me.Close()
        End If

    End Sub
    Private Sub Close_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Close_ImageButton.Click
        Me.Close()
    End Sub
    Private Sub GenerateCustomerNumber_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles GenerateCustomerNumber_ImageButton.Click

        Dim sysman As New SysManager(AppSettings.LoggedUser)
        Me.iMainDataObject.CustomerNo = sysman.Get_SequenceNo(SequenceNumeringNamesEnum.CustomerNumberSequence)
        Me.CustomerNo_TextBox.Text = Me.iMainDataObject.CustomerNo

    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Refresh_FixLists()
        IdType_StylishComboBox.Items.Clear()
        Dim ss() As String = [Enum].GetNames(GetType(IdTypesEnum))
        For Each s As String In ss
            IdType_StylishComboBox.Items.Add(s)
        Next
        If IdType_StylishComboBox.Items.Count > 0 Then IdType_StylishComboBox.SelectedItem = ss(0)
    End Sub
    Private Sub Refresh_FormCaptions()
        If (Me.iMainDataObject IsNot Nothing) AndAlso (Me.iMainDataObject.Name <> "") Then
            Me.Text = "Customer Edit - " & Me.iMainDataObject.Name
        Else
            Me.Text = "Customer Edit"
        End If
    End Sub
    Private Sub UpdateCustomerRecord()

        Refresh_DataObjectWithControls()
        Dim sm As New SvcManager(AppSettings.LoggedUser)
        sm.Update_Customer(Me.iMainDataObject)

    End Sub
    Private Sub DeleteCustomerRecord()

        Refresh_DataObjectWithControls()
        Dim sm As New SvcManager(AppSettings.LoggedUser)
        sm.Delete_Customer(Me.iMainDataObject)

    End Sub
#End Region

    Enum IdTypesEnum
        NID
        Passport
        DriversLicense
        WorkPermit
    End Enum







End Class